package com.solr;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

import java.awt.event.HierarchyListener;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program: Solr
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-11
 **/
public class Solrfuxi {

    private static final String BASE_URL = "http://192.168.117.128:8080/solr/collection1";

    public static HttpSolrClient getSoleClient() {
        return new HttpSolrClient.Builder(BASE_URL).build();
    }

    //            "product_catalog_name": "幽默杂货",
//                "product_price": 18.9,
//                "product_name": "花儿朵朵彩色金属门后挂 8钩免钉门背挂钩2066",
//                "id": "1",
//                "product_picture": "2014032613103438.png",
    @Test
    public void addDoc() throws IOException, SolrServerException {
        HttpSolrClient solrClient = getSoleClient();
        SolrInputDocument solrInputFields = new SolrInputDocument();
        solrInputFields.addField("product_catalog_name", "日用百货");
        solrInputFields.addField("product_price", "123");
        solrInputFields.addField("id", "123");
        solrInputFields.addField("product_picture", "123123.jpg");
        solrClient.add(solrInputFields);
        solrClient.commit();
    }

    @Test
    public void getById() throws IOException, SolrServerException {
        HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(BASE_URL).build();

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q", "product_catalog_name:日用百货");
        QueryResponse query = httpSolrClient.query(solrQuery);
        SolrDocumentList results = query.getResults();
        for (SolrDocument result : results) {
            System.out.println(result.getFieldValue("product_catalog_name"));
        }
    }

    @Test
    public void delDoc() throws IOException, SolrServerException {
        HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(BASE_URL).build();
        httpSolrClient.deleteByQuery("product_catalog_name:日用百货", 10);
    }

    @Test
    public void getByQuery() throws IOException, SolrServerException {
        HttpSolrClient build = new HttpSolrClient.Builder(BASE_URL).build();
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q", "product_catalog_name:时尚卫浴 AND product_name:时尚");
        solrQuery.set("sort", "product_price desc");
        solrQuery.setHighlight(true);
        solrQuery.setHighlightSimplePre("<span>");
        solrQuery.setHighlightSimplePost("</span>");
        solrQuery.addHighlightField("product_catalog_name");
        solrQuery.addHighlightField("product_name");
        solrQuery.setStart(0);
        solrQuery.setRows(10);
        QueryResponse query = build.query(solrQuery);

        Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();

        SolrDocumentList results = query.getResults();
        System.out.println(results.getNumFound());
        for (SolrDocument result : results) {
            System.out.println(result.getFieldValue("product_catalog_name"));
            System.out.println(result.getFieldValue("product_price"));
            System.out.println(result.getFieldValue("product_name"));
            System.out.println(result.getFieldValue("id"));
            System.out.println("---获取高亮---");
            Map<String, List<String>> id = highlighting.get(result.getFieldValue("id"));
            for (String product_catalog_name : id.get("product_catalog_name")) {
                System.out.println(product_catalog_name);
            }
            for (String product_catalog_name : id.get("product_name")) {
                System.out.println(product_catalog_name);
            }
        }
    }
}
